import React from 'react';
import { Card, Tabs, Form, Input, Button, Space, Switch, Select, InputNumber, Divider, message, Alert, Popconfirm } from 'antd';
import { SaveOutlined, SyncOutlined, UndoOutlined, ExportOutlined, ImportOutlined, DeleteOutlined, WarningOutlined } from '@ant-design/icons';

// 创建一个简单的设置存储
interface SettingsState {
  systemName: string;
  systemLogo: string;
  itemsPerPage: number;
  dateFormat: string;
  language: string;
  enableDarkMode: boolean;
  enableNotifications: boolean;
  apiEndpoint: string;
  saveSettings: (settings: Partial<SettingsState>) => void;
}

// 使用简单对象模拟存储
const settingsStore = {
  systemName: '企业知识库管理平台',
  systemLogo: '/logo.png',
  itemsPerPage: 10,
  dateFormat: 'YYYY-MM-DD',
  language: 'zh-CN',
  enableDarkMode: false,
  enableNotifications: true,
  apiEndpoint: 'https://api.example.com',
  saveSettings: (settings: Partial<SettingsState>) => {
    Object.assign(settingsStore, settings);
    message.success('设置已保存');
  }
};

const useSettingsStore = () => settingsStore;

const SystemSettings: React.FC = () => {
  const settings = useSettingsStore();
  const [form] = Form.useForm();
  
  const handleSave = () => {
    form.validateFields().then(values => {
      settings.saveSettings(values);
    });
  };
  
  const handleReset = () => {
    form.resetFields();
    message.info('已重置为当前设置');
  };
  
  const handleExport = () => {
    const settingsJson = JSON.stringify(settings);
    const blob = new Blob([settingsJson], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    
    const a = document.createElement('a');
    a.href = url;
    a.download = 'knowledge-platform-settings.json';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    
    message.success('设置已导出');
  };
  
  const handleImport = () => {
    // 实际项目中实现导入功能
    message.success('设置已导入');
  };
  
  return (
    <Card
      title="系统设置"
      extra={
        <Space>
          <Button icon={<UndoOutlined />} onClick={handleReset}>重置</Button>
          <Button type="primary" icon={<SaveOutlined />} onClick={handleSave}>保存</Button>
        </Space>
      }
    >
      <Form
        form={form}
        layout="vertical"
        initialValues={settings}
      >
        <Tabs
          items={[
            {
              key: 'general',
              label: '基本设置',
              children: (
                <>
                  <Form.Item name="systemName" label="系统名称" rules={[{ required: true }]}>
                    <Input placeholder="请输入系统名称" />
                  </Form.Item>
                  
                  <Form.Item name="systemLogo" label="系统Logo">
                    <Input placeholder="请输入Logo URL" />
                  </Form.Item>
                  
                  <Form.Item name="itemsPerPage" label="每页显示条目">
                    <InputNumber min={5} max={100} />
                  </Form.Item>
                  
                  <Form.Item name="dateFormat" label="日期格式">
                    <Select>
                      <Select.Option value="YYYY-MM-DD">YYYY-MM-DD</Select.Option>
                      <Select.Option value="DD/MM/YYYY">DD/MM/YYYY</Select.Option>
                      <Select.Option value="MM/DD/YYYY">MM/DD/YYYY</Select.Option>
                    </Select>
                  </Form.Item>
                  
                  <Form.Item name="language" label="系统语言">
                    <Select>
                      <Select.Option value="zh-CN">简体中文</Select.Option>
                      <Select.Option value="en-US">English</Select.Option>
                    </Select>
                  </Form.Item>
                </>
              )
            },
            {
              key: 'storage',
              label: '存储配置',
              children: (
                <>
                  <Alert
                    message="存储配置"
                    description="配置系统文档和数据的存储方式"
                    type="info"
                    showIcon
                    style={{ marginBottom: 16 }}
                  />
                  
                  <Form.Item name="storageType" label="存储类型">
                    <Select>
                      <Select.Option value="local">本地存储</Select.Option>
                      <Select.Option value="s3">AWS S3</Select.Option>
                      <Select.Option value="oss">阿里云OSS</Select.Option>
                    </Select>
                  </Form.Item>
                  
                  <Form.Item name="storagePath" label="存储路径">
                    <Input placeholder="请输入存储路径" />
                  </Form.Item>
                </>
              )
            },
            {
              key: 'retrieval',
              label: '检索设置',
              children: (
                <>
                  <Form.Item name="embeddingModel" label="向量模型">
                    <Select>
                      <Select.Option value="openai">OpenAI</Select.Option>
                      <Select.Option value="huggingface">HuggingFace</Select.Option>
                      <Select.Option value="custom">自定义</Select.Option>
                    </Select>
                  </Form.Item>
                  
                  <Form.Item name="vectorDbType" label="向量数据库">
                    <Select>
                      <Select.Option value="pinecone">Pinecone</Select.Option>
                      <Select.Option value="milvus">Milvus</Select.Option>
                      <Select.Option value="faiss">FAISS</Select.Option>
                    </Select>
                  </Form.Item>
                  
                  <Form.Item name="chunkSize" label="文档分块大小">
                    <InputNumber min={100} max={2000} />
                  </Form.Item>
                </>
              )
            },
            {
              key: 'security',
              label: '安全设置',
              children: (
                <>
                  <Form.Item name="enableMFA" label="启用多因素认证" valuePropName="checked">
                    <Switch />
                  </Form.Item>
                  
                  <Form.Item name="sessionTimeout" label="会话超时时间(分钟)">
                    <InputNumber min={5} max={1440} />
                  </Form.Item>
                  
                  <Form.Item name="passwordPolicy" label="密码策略">
                    <Select>
                      <Select.Option value="simple">简单</Select.Option>
                      <Select.Option value="medium">中等</Select.Option>
                      <Select.Option value="strong">强</Select.Option>
                    </Select>
                  </Form.Item>
                </>
              )
            },
            {
              key: 'advanced',
              label: '高级设置',
              children: (
                <>
                  <Form.Item name="apiEndpoint" label="API端点">
                    <Input placeholder="请输入API端点" />
                  </Form.Item>
                  
                  <Form.Item name="logLevel" label="日志级别">
                    <Select>
                      <Select.Option value="debug">Debug</Select.Option>
                      <Select.Option value="info">Info</Select.Option>
                      <Select.Option value="warn">Warn</Select.Option>
                      <Select.Option value="error">Error</Select.Option>
                    </Select>
                  </Form.Item>
                  
                  <Divider />
                  
                  <Space direction="vertical" style={{ width: '100%' }}>
                    <Button 
                      icon={<ExportOutlined />} 
                      onClick={handleExport}
                      block
                    >
                      导出设置
                    </Button>
                    
                    <Button 
                      icon={<ImportOutlined />} 
                      onClick={handleImport}
                      block
                    >
                      导入设置
                    </Button>
                    
                    <Popconfirm
                      title="确定要重置所有设置为默认值吗？"
                      icon={<WarningOutlined style={{ color: 'red' }} />}
                      onConfirm={() => {
                        // 实际项目中实现重置功能
                        message.success('已重置为默认设置');
                      }}
                    >
                      <Button 
                        danger 
                        icon={<DeleteOutlined />}
                        block
                      >
                        重置为默认设置
                      </Button>
                    </Popconfirm>
                  </Space>
                </>
              )
            }
          ]}
        />
      </Form>
    </Card>
  );
};

export default SystemSettings; 